﻿using UnityEngine;
using System.Collections;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
using PersonInfo;

public class ProtobufTest1 : MonoBehaviour
{

	private const string Path = "C://data";
	void Start()
	{
		Person p1 = new Person() { Name = "小明", Gender = true };
		Person p2 = new Person() { Name = "小红", Gender = false };
		List<Person> persons = new List<Person>() { p1, p2 };
		//序列化存储
		Protobuf.ProtobufHelper.SerializeToFile<List<Person>>(Path, persons);

		//或者
		//string content = Protobuf.ProtobufHelper.Serialize<List<Person>>(persons);
		//File.WriteAllText(Path, content);

		//将数据从文件中读取出来，反序列化
		List<Person> Persons1;
		using (Stream file = File.OpenRead(Path))
		{
			Persons1 = Protobuf.ProtobufHelper.DeSerialize<List<Person>>(file);
		}

		//或者
		//List<Person> Persons1 = Protobuf.ProtobufHelper.DeSerialize<List<Person>>(content);
		foreach (Person p in Persons1)
			Debug.Log(p.ToString());
	}
}
